//
//  StatusOriginalView.swift
//  微博Swift
//
//  Created by  justinchou on 16/2/29.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit
/// 微博头像大小
private let JustinIconViewSize = CGSize(width: 36, height: 36)

class StatusOriginalView: UIView {

    // MARK: - 公共属性
    // 设置控件的内容
    var viewModel: StatusViewModel? {
        didSet{
            //设置圆角头像
            iconView.justin_setImageWithURL(viewModel?.status.user?.profile_image_url, placeholderName: "avatar_default_big", imageSize: JustinIconViewSize, cornerRadius: nil)
            
            //设置会员认证信息
            memberImageView.image = viewModel?.userMemberImage
            vipImageView.image = viewModel?.userVerifyImage
            //信息来源
            sourceLabel.text = viewModel?.sourceText
            //创建微博时间
            timeLabel.text = viewModel?.createTime?.justin_dateDescription
            
            nameLabel.text = viewModel?.status.user?.screen_name
            contentLabel.text = viewModel?.status.text
            
            //更新配图视图约束
            //根据配图视图大小来判断有无图片，并且修改约束
            let offset = (viewModel?.pictureViewSize == CGSizeZero) ? 0 : viewModel!.cellLayout.margin
            picturesView.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(viewModel!.pictureViewSize)
                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            }
            
            picturesView.pic_urls = viewModel?.status.pic_urls
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有控件
    /// 头像图片
    private let iconView: UIImageView = UIImageView(justin_imageName: "avatar_default_big")
    /// 新浪昵称
    private let nameLabel: UILabel = UILabel(title: "新浪昵称", fontSize: 14)
    /// 会员图标
    private let memberImageView: UIImageView = UIImageView(justin_imageName: "common_icon_membership_level1")
    /// VIP认证图标
    private let vipImageView: UIImageView = UIImageView(justin_imageName: "avatar_grassroot")
    /// 时间标签
    private let timeLabel: UILabel = UILabel(title: "刚刚", color: UIColor.orangeColor(), fontSize: 10)
    /// 来源标签
    private let sourceLabel: UILabel = UILabel(title: "来自 火星", fontSize: 10)
    /// 微博正文
    private let contentLabel: UILabel = UILabel(title: "微博详细内容。。。。微博详细内容。。。。微博详细内容。。。。微博详细内容。。。。微博详细内容。。。。", fontSize: 15)
    /// 配图视图
    private let picturesView: StatusPicturesView = StatusPicturesView()
    

}

// MARK: - 设置原创微博的界面
extension StatusOriginalView {

    private func setupUI() {
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picturesView)
        
        let margin: CGFloat = 12
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(margin)
            make.left.equalTo(self).offset(margin)
            make.size.equalTo(CGSizeMake(36, 36))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(margin)
        }
        
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right).offset(-4)
            make.centerY.equalTo(iconView.snp_bottom).offset(-4)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(margin)
        }
        
        contentLabel.textAlignment = .Left
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView)
            make.right.equalTo(self).offset(-margin)
            //将左右设置约束后，文本才能换行
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            
        }
        
        picturesView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            make.size.equalTo(CGSizeZero)
            
            // MARK: - 必须要有个向下的约束，才能执行自动计算行高
            make.bottom.equalTo(self).offset(-margin)
        }
        
    }
}
